class OtherUtils {
    static Copy(text) {
        var input = document.createElement('input');
        input.setAttribute('readonly', 'readonly'); // 防止手机上弹出软键盘
        input.setAttribute('value', text);
        document.body.appendChild(input);
        // input.setSelectionRange(0, 9999);
        input.select();
        var res = document.execCommand('copy');
        document.body.removeChild(input);
        return res;
    }
    static RandomId(p) {
        if (!p) {
            p = 'id'
        }
        return `${p}_${parseInt(Math.random() * 1000)}_${new Date().getTime()}`;
    }
    static ShowTip(text,time,width) {
        if (window.showTip___created) {
            window.showTip___created(text,time,width)
        } else {
            window.showTip___created = (function () {
                let div = document.createElement('div');
                div.style.top = '-30px';
                div.style.width = '100px';
                div.style.height = '30px';
                div.style.left = 'calc(50vw - 50px)';
                div.style.lineHeight = '30px';
                div.style.position = 'fixed';
                div.style.background = '#00bcd442';
                div.style.borderRadius = '0 0 5px 5px';
                div.style.textAlign = 'center';
                div.style.transition = 'top 1s';
                document.body.appendChild(div);
                let id = null;
                return function (text,time,width) {
                    if (width) {
                        div.style.width = `${width}px`;
                        div.style.left = `calc(50vw - ${width / 1}px)`;
                    }
                    div.innerHTML = text;
                    div.style.top = '0px';
                    if (id) {
                        clearTimeout(id);
                    }
                    id = setTimeout(() => {
                        div.style.top = '-30px';
                        id = null;
                    },time || 5000);
                }
            })();
            setTimeout(() => {
                window.window.showTip___created(text);
            },500);
        }
    }
    static Rnd(n) {
        return parseInt(Math.random() * n);
    }
    static colors = ['#49fa32', '#37A2FF', '#FF0087', '#FFBF00']
    // 创建一个 10 * 10 的矩阵
    static makeSXS(ctx,width,height) {
        let colors = OtherUtils.colors;
        let xsize = parseInt(width / 10),
            ysize = parseInt(height / 10);
        for (let i = 0;i < 20;i++) {
            let n = OtherUtils.Rnd(1024).toString(2);
            while (n.length < 10) {
                n = '0' + n;
            }
            let l = i * 5;
            for (let j = 0;j < 10;j += 2) {
                ctx.fillStyle = colors[parseInt(n.substring(j,j + 2),2)];
                ctx.fillRect(parseInt(l / 10) * 10,(l % 10) * 10,xsize,ysize);
                l++
            }
        }
        // return arr;
    }
    static toLen(n,len) {
        n = n + '';
        while (n.length < len) {
            n = '0' + n;
        }
        return n;
    }
    static OpenFile() {
        return new Promise(s => {
            let inp = document.createElement('input');
            inp.style.display = 'none';
            inp.type = 'file';
            document.body.appendChild(inp);
            inp.click();
            inp.onchange = function () {
                if (inp.files.length) {
                    /// read file content.
                    var reader = new FileReader();
                    reader.readAsText(inp.files[0]);
                    reader.onloadend = function(){
                        inp.remove();
                        s(this.result);
                    }
                } else {
                    inp.remove();
                    s(null);
                }
            }
        })
    }
}
